package com.david.mydemo.framework.config;

import com.david.mydemo.ctl_webservice.UserInfoWebService;
import com.david.mydemo.ctl_webservice.UserInfoWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;

import javax.xml.ws.Endpoint;

@Configuration
@EnableWs
public class WebServiceConfig extends WsConfigurerAdapter {

    /**
     * WebService访问的父路径，可以找到所有wsdl文件
     * <p>注入servlet bean name不能dispatcherServlet ,否则会覆盖dispatcherServlet</p>
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean<CXFServlet> cxfServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/webservice/*");
    }

    @Bean
    public UserInfoWebService userInfoWebService() {
        return new UserInfoWebServiceImpl();
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        // 参数二，是SEI实现类对象
        EndpointImpl endpoint = new EndpointImpl(this.springBus(), this.userInfoWebService());
        // 发布服务
        endpoint.publish("/userWebService");
        return endpoint;
    }

//    @Bean
//    public Jaxb2Marshaller marshaller() {
//        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
//        // 设置WebService的包名
//        marshaller.setContextPath("com.cop.console.webService");
//        return marshaller;
//    }
//
//    @Bean
//    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
//        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
//        webServiceTemplate.setMarshaller(marshaller);
//        webServiceTemplate.setUnmarshaller(marshaller);
//        // 设置WebService地址
//        webServiceTemplate.setDefaultUri("http://example.com/webservice");
//        return webServiceTemplate;
//    }

}
